package com.fang.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fang.common.wrapper.ResponseWrapper;
import com.fang.entity.SysRole;
import com.fang.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Fang Ruichuan
 * @since 2022-08-27
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController {

    @GetMapping("/info/{id}")
    public ResponseWrapper info(@PathVariable("id") Long id) {
        SysUser sysUser = sysUserService.getById(id);
        Assert.notNull(sysUser, "找不到该管理员");
        List<SysRole> roles = sysRoleService.listRolesByUserId(id);
        sysUser.setSysRoles(roles);
        return ResponseWrapper.success(sysUser);
    }

    @GetMapping("/list")
    public ResponseWrapper list(String username) {
        Page<SysUser> pageData = sysUserService.page(getPage(), Wrappers.<SysUser>lambdaQuery()
                .like(StringUtils.isNotBlank(username), SysUser::getUsername, username));
        pageData.getRecords().forEach(u -> {
            u.setSysRoles(sysRoleService.listRolesByUserId(u.getId()));
        });
        return ResponseWrapper.success(pageData);
    }

}
